package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.controller.form.ProjectNewroleForm;
import com.woniuxy.controller.form.ProjectroleStateForm;
import com.woniuxy.controller.info.ProjectroleInfo;
import com.woniuxy.model.ProjectRole;
import com.woniuxy.dao.ProjectRoleDao;
import com.woniuxy.service.ProjectRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.MyException;

import com.woniuxy.utils.TimeMethodsutil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Service
public class ProjectRoleServiceImpl extends ServiceImpl<ProjectRoleDao, ProjectRole> implements ProjectRoleService {

    private ObjectMapper om = new ObjectMapper();

    @Resource
    private ProjectRoleDao projectRoleDao;

    @Override
    public boolean addprojectrole(ProjectNewroleForm projectNewroleForm) throws MyException {

        List<ProjectRole> projectRoles = projectRoleDao.selectList(new QueryWrapper<ProjectRole>().eq("project_role_num", projectNewroleForm.getRolenum()).or().eq("project_role_name", projectNewroleForm.getRolename()));
        if(null != projectRoles && projectRoles.size()>0) throw new MyException(500,"角色或编号已存在");

        ProjectRole projectRole = new ProjectRole();
        projectRole.setProjectRoleNum(projectNewroleForm.getRolenum());
        projectRole.setProjectRoleName(projectNewroleForm.getRolename());
        projectRole.setSystemSign(projectNewroleForm.getSystemName());

        String s = "";
        try {
            s = om.writeValueAsString(projectNewroleForm.getTypes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        projectRole.setProjectRoleStates(s);
        projectRole.setProjectRoleState(1);
        projectRole.setProjectRoleCreatename(projectNewroleForm.getUsername());
        projectRole.setProjectRoleCreatetime(TimeMethodsutil.getNowTimeToString());
        projectRole.setSystemSign(projectNewroleForm.getSystemName());
//        projectRole.setProjectId(projectNewroleForm.getProjectId());

        int insert = projectRoleDao.insert(projectRole);
        //boolean b = projectRoleService.saveOrUpdate(projectRole, new QueryWrapper<ProjectRole>().eq("project_role_name", projectNewroleForm.getRolename()).eq("project_id",projectNewroleForm.getProjectId()));

        return insert>0? true:false;
    }

    @Override
    public List<ProjectroleInfo> getRolesByPid() {

        List<ProjectRole> projectRoles = projectRoleDao.getRolesByPid();
        List<ProjectroleInfo> projectroleInfos = CopyUtil.copyList(projectRoles, ProjectroleInfo.class);
        List<String> list = null;
        for (int i = 0; i < projectRoles.size(); i++) {
            String s = "";
            List<String> strings = null;
            try {
                strings = om.readValue(projectRoles.get(i).getProjectRoleStates(), List.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            System.out.println(strings);
            for (int j = 0; j < strings.size(); j++) {
                if(j == strings.size()-1){
                    s+=strings.get(j);
                }else{
                    s+=(strings.get(j)+",");
                }
            }
            System.out.println(s);
            if(projectRoles.get(i).getProjectRoleState()==1){
                projectroleInfos.get(i).setProjectRoleState("有效");
            }else if(projectRoles.get(i).getProjectRoleState()==0){
                projectroleInfos.get(i).setProjectRoleState("禁用");
            }
            projectroleInfos.get(i).setProjectRoleStates(s);
        }

        return projectroleInfos;
    }

    @Override
    public boolean changeroles(ProjectNewroleForm projectNewroleForm) throws Exception {

        ProjectRole projectRole = new ProjectRole();
        projectRole.setProjectRoleNum(projectNewroleForm.getRolenum());
        projectRole.setProjectRoleName(projectNewroleForm.getRolename());
        projectRole.setProjectRoleStates(om.writeValueAsString(projectNewroleForm.getTypes()));
        projectRole.setProjectRoleChangename(projectNewroleForm.getUsername());
        projectRole.setProjectRoleChangetime(TimeMethodsutil.getNowTimeToString());

        QueryWrapper<ProjectRole> wrapper = new QueryWrapper<>();
        wrapper.eq("project_role_num",projectNewroleForm.getRolenum());
//        wrapper.eq("project_id",projectNewroleForm.getProjectId()).eq("project_role_id",projectNewroleForm.getProjectRoleId());

        int update = projectRoleDao.update(projectRole, wrapper);
        return update>0? true:false;
    }

    @Override
    public boolean changestate(ProjectroleStateForm projectroleStateForm) {

        QueryWrapper<ProjectRole> wrapper = new QueryWrapper<>();
        wrapper.eq("project_role_id",projectroleStateForm.getProjectRoleId());

        ProjectRole projectRole = new ProjectRole();
        if(projectroleStateForm.getProjectRoleState().equals("禁用")){
            projectRole.setProjectRoleState(1);
        }else if(projectroleStateForm.getProjectRoleState().equals("有效")){
            projectRole.setProjectRoleState(0);
        }

        int update = projectRoleDao.update(projectRole, wrapper);
        return update>0? true:false;
    }

    @Override
    public boolean removesome(List<Integer> list) {

        QueryWrapper<ProjectRole> wrapper = new QueryWrapper<>();
        wrapper.in("project_role_id",list);

        int delete = projectRoleDao.delete(wrapper);

        return delete>0? true:false;
    }

    @Override
    public List<ProjectroleInfo> getRoles() {
        //new QueryWrapper<ProjectRole>().eq("project_role_delect", 0)
        List<ProjectRole> projectRoles = this.baseMapper.selectList(new QueryWrapper<ProjectRole>().eq("project_role_delect", 0).orderByAsc("project_role_num"));
        List<ProjectroleInfo> projectroleInfos = CopyUtil.copyList(projectRoles, ProjectroleInfo.class);
        List<String> list = null;
        for (int i = 0; i < projectRoles.size(); i++) {
            String s = "";
            List<String> strings = null;
            try {
                strings = om.readValue(projectRoles.get(i).getProjectRoleStates(), List.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            System.out.println(strings);
            for (int j = 0; j < strings.size(); j++) {
                if(j == strings.size()-1){
                    s+=strings.get(j);
                }else{
                    s+=(strings.get(j)+",");
                }
            }
            System.out.println(s);
            if(projectRoles.get(i).getProjectRoleState()==1){
                projectroleInfos.get(i).setProjectRoleState("有效");
            }else if(projectRoles.get(i).getProjectRoleState()==0){
                projectroleInfos.get(i).setProjectRoleState("禁用");
            }
            projectroleInfos.get(i).setProjectRoleStates(s);
        }
        return projectroleInfos;
    }

    @Override
    public List<ProjectroleInfo> getManagerRole() {
        List<ProjectroleInfo> projectroleInfos=projectRoleDao.selectManagerRole();
        return projectroleInfos;
    }
}
